<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>table模块快速使用</title>
  <link rel="stylesheet" href="js/layui/css/layui.css" media="all">
</head>
<body>

<form class="layui-form" action="">
  <div class="layui-form-item">
    <div class="layui-inline">
      <label class="layui-form-label">状态</label>
      <div class="layui-input-inline" style="width: 100px;">
        <select name="state" >
          <option value="">请选择</option>
          <option value="0">已付款</option>
          <option value="1">已完成</option>
          <option value="2">已取消</option>
        </select>
      </div>
    </div>
    <div class="layui-inline">
      <button class="layui-btn" lay-submit lay-filter="formDemoSearch">搜索</button>
    </div>
  </div>
</form>



<!--表格容器-->
<table id="demo" lay-filter="test"></table>

<!--表格容器 订单详情-->
<div id="detailDiv" style="display: none">
  <table id="demoDetail" lay-filter="testDetail" ></table>
</div>



<!--行内工具栏：-->
<script type="text/html" id="toolbarDemo2">
  <div class="layui-btn-container">
    <button class="layui-btn layui-btn-sm layui-btn-danger" lay-event="detail">订单详情</button>
  </div>
</script>




<!--脚本开始-->
<script src="js/layui/layui.js"></script>
<script>
  layui.use(['table','form'], function(){
    var table = layui.table;
    var form = layui.form;
    var $ = layui.$;




    //搜索的表单监听提交
    form.on('submit(formDemoSearch)', function(data){
      //layer.msg(JSON.stringify(data.field));
      table.reload("idTest",{where:data.field});
      return false;
    });




    //行内工具条触发事件
    table.on('tool(test)', function(obj){
      switch(obj.event){
        case 'detail':
          //layer.msg('查看详情');
          layer.open({
            type: 1,
            shadeClose:true,
            content: $('#detailDiv') //这里content是一个DOM，注意：最好该元素要存放在body最外层，否则可能被其它的相对元素所影响
          });


          //渲染订单详情
          table.render({
            elem: '#demoDetail'
            ,id:"idTest2"
            ,url: 'order-detail/getList?orderId='+obj.data.id //数据接口
            ,page: true //开启分页
            ,cols: [[ //表头
              {field: 'id', title: 'ID', width:80}
              ,{field: 'orderId', title: '订单编号', width:80}
              ,{field: 'proName', title: '菜品名', width:180}
              ,{field: 'num', title: '数量', width:80}
            ]]
          });
          break;
      };
    });





    //第一个实例
    table.render({
      elem: '#demo'
      ,id:"idTest"
      ,url: 'orders/getList' //数据接口
      ,page: true //开启分页
      ,cols: [[ //表头
        {field: 'id', title: 'ID', width:80}
        ,{field: 'deskId', title: '桌号', width:80}
        ,{field: 'state', title: '订单状态', width:100 ,templet: function(d){
            if(d.state==0){
              return '<span style="color: #c00;">已付款</span>'
            }else if(d.state==1){
              return '<span style="color: #0c0;">已完成</span>'
            }else if(d.state==2){
              return '<span style="color: #CCC;">已取消</span>'
            }

          }}
        ,{field: 'createTime', title: '下单时间', width:280}
        ,{title: '操作', width:280,toolbar: '#toolbarDemo2'}
      ]]
    });

  });
</script>
</body>
</html>